<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <todo-header @add="addfn" />
    <!-- 主体部分 -->
    <todo-main :list="isshowlist" @del="delfn" @change="changefn" @checkall="checkallfn"/>
    <!-- 底部部分 -->
    <todo-footer :list="isshowlist" @clear="clearfn" :type="type" @changetype="changetypefn"/>
  </section>
</template>

<script>
import TodoFooter from "./components/TodoFooter.vue";
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
export default {
  components: {
    TodoFooter,
    TodoHeader,
    TodoMain,
  },
  data() {
    return {
      // list: [
      //   { id: 1, name: "吃饭", isDone: true },
      //   { id: 2, name: "睡觉", isDone: false },
      //   { id: 3, name: "打豆豆", isDone: true },
      // ],
      list:JSON.parse(localStorage.getItem('localist')),
      type:"all"
    };
  },
  computed:{
    isshowlist(){
      if (this.type==='active') {
        return this.list.filter(item=>item.isDone===false)
      }else if (this.type==='completed') {
        return this.list.filter(item=>item.isDone===true)
      }else {
        return this.list
      }
    }
  },
  watch:{
    list:{
      deep:true,
      handler(newValue){
        localStorage.setItem('localist',JSON.stringify(newValue))
      } 
    }
  },
  methods: {
    delfn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    addfn(name){
      this.list.push({
        id:Date.now(),
        name:name,
        isDone:false
      })
    },
    changefn(id){
      const result=this.list.find(item=>item.id===id).isDone
      this.list.find(item=>item.id===id).isDone=!result
    },
    clearfn(){
      this.list = this.list.filter((item) => item.isDone ===false);
    },
    changetypefn(type){
      this.type=type
    },
    checkallfn(vul){
      this.list.forEach(item=>item.isDone=vul);
    }
  },
};
</script>

<style></style>
